<template>
  <view class="ver-layout">
    <!--  上面的布局  -->
    <view class="top-layout hor-layout-side-center">
      <view class="top-title">{{ selClassType.label }}
<!--        <u-tag style="margin-left: 10px;" size="mini" :text="`共${loadingObj.totalNum}个`"></u-tag>-->
      </view>
      <view class="select-type-view" @click="showClassType=true"> 分类
        <i class="iconfont select-icon">&#xe771;</i>
      </view>
    </view>
    <u-select v-model="showClassType" :list="majorList" :default-value="selClassTypeIndex" title="乐器类别"
              @confirm="selectClassType"></u-select>
    <!-- 下面的列表   -->
    <view class="class-list-view">
      <view v-if="classList.length<=0" class="list-no-data">暂无数据</view>
      <block v-else>
        <view v-for="(item,index) in classList" :key="index" class="class-item ver-layout" @click="toStudentPage(item)">
          <view class="hor-layout">
            <view class="class-title">{{ item.class_title }}</view>
            <view class="class-num">{{ item.student_total }}人</view>
          </view>
          <view class="hor-layout" style="margin-top: 18px">
            <!--          <view class="class-teacher">教师:王可可</view>-->
            <view class="class-time">开班时间{{ item.start_time }}</view>
            <view class="class-type">{{ item.class_major }}</view>
          </view>
        </view>
      </block>
    </view>
    <u-loadmore v-if="loadingObj.status" margin-top="15" :status="loadingObj.status"/>
    <!-- 添加按钮   -->
    <view class="add-student-btn" @click="toAddClassPage">
      <i class="iconfont add-student-icon">&#xe601;</i>
    </view>
  </view>
</template>

<script>
import {studentList, createClass} from "@/router.js"
import {SelectClassApi} from "@/api/enterStudent.js"

let engine = new SelectClassApi()
export default {
  name: "classList",
  data() {
    return {
      loadingObj: {
        status: '',
        current: 1,
        total: ''
      },
      loadingStatus: '',
      showClassType: false,
      selClassType: {},
      majorList: [],
      classList: [{}]
    }
  },
  computed: {
    selClassTypeIndex() {
      let index = this.majorList.findIndex(item => item.value === this.selClassType.value)
      return [index]
    }
  },
  onLoad() {
    this.getMajorList()
  },
  onReachBottom(e) {
    this.loadMore()
  },
  methods: {
    getMajorList() {
      engine.getMajorList().then(res => {
        this.majorList = res.data.map(item => {
          return {value: item.id, label: item.name}
        })
        this.selClassType = this.majorList[0]
        this.refreshData()
      })
    },
    toAddClassPage() {
      this.$u.route({url: createClass})
    },
    selectClassType(val) {
      this.selClassType = val[0]
      this.refreshData()
    },
    toStudentPage(item) {
      this.$u.route({url: studentList, params: item})
    },
    refreshData() {
      this.loadingObj.current = 1
      this.loadingObj.status = ''
      this.classList = []
      this.getClassList()
    },
    loadMore() {
      if (this.loadingObj.current === this.loadingObj.total) {
        this.loadingObj.status = 'nomore'
      } else {
        this.loadingObj.status = 'loading'
        this.loadingObj.current = this.loadingObj.current + 1
        this.getClassList()
      }
    },
    loadFinish(total) {
      this.loadingObj.totalNum = total
      this.loadingObj.total = Math.ceil(total / 10)
      this.loadingObj.status = ''
      if (this.loadingObj.current >= this.loadingObj.total
          && this.classList.length > 0) {
        this.loadingObj.status = 'nomore'
      }
    },
    getClassList() {
      let major = this.selClassType.value || ''
      let page = this.loadingObj.current
      engine.getClassList(page, major).then(res => {
        this.classList = this.classList.concat(res.data)
        this.loadFinish(res.total)
      }).catch(err => {
        this.$u.toast(err.message);
      })
    }
  }
}
</script>

<style>
page {
  background: #F2F2F2;
}

.top-layout {
  position: fixed;
  width: 100%;
  height: 56px;
  padding: 0px 12px;
  background: white;
  border-bottom: 1px solid #F2F2F2;
}

.top-title {
  font-size: 14px;
  color: #333333;
}

.select-type-view {
  height: 32px;
  padding: 0px 10px;
  line-height: 32px;
  color: #666666;
  background: #F4F8FB;
  border: 1px solid #E5E6E7;
  border-radius: 3px;
}

.select-icon {
  color: #666666;
  font-size: 15px;
  margin-left: 7px;
}

.class-list-view {
  margin-top: 56px;
  margin-bottom: 10px;
}

.class-item {
  margin: 0px 12px;
  margin-top: 18px;
  padding: 18px 12px;
  background: #FFFFFF;
  border-radius: 6px;
}

.class-title {
  flex: 1;
  font-size: 16px;
  color: #000000;
}

.class-num {
  font-size: 12px;
  color: #333333;
}

.class-teacher {
  font-size: 14px;
  color: #000000;
}

.class-time {
  flex: 1;
  /*margin-left: 13px;*/
  font-size: 14px;
  color: #999999;
}

.class-type {
  width: 84px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  background: #FDC62D;
  border-radius: 12px;
  /*文本样式*/
  font-size: 14px;
  color: #FFFFFF;
}

.add-student-btn {
  position: fixed;
  bottom: 33px;
  right: 13px;
  width: 58px;
  height: 58px;
  line-height: 58px;
  text-align: center;
  background: #6079F5;
  border-radius: 50%;
}

.add-student-icon {
  color: white;
  font-size: 30px;
}
</style>
